﻿using DoNetDrive.Core.Command;
using Face8280WebSocketServer.Utility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Face8280WebSocketServer.Command.Remote
{
   /// <summary>
   /// 上传系统文件（屏保，开机图片）
   /// </summary>
    internal class PushSystemFile : JsonCommand
    {
        byte[] data;
        public PushSystemFile(INCommandDetail cd, Models.SystemFileType fileType, Models.FeatureDataModel data, string user) : base(cd, null)
        {
            CheckVal(data, user);
            mSendObject = new { cmd = Reply, name = fileType.ToString() , user , data };
            mJsonResult = (s) => _Result = new CommandResult(s);
        }
        /// <summary>
        /// 创建待发送的数据包
        /// </summary>
        protected override void CreatePacket()
        {
            var setting = new JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            };
            _Packet = new JsonPacket(JsonConvert.SerializeObject(mSendObject, setting), data);
        }
        /// <summary>
        /// 检查参数
        /// </summary>
        /// <param name="featureData"></param>
        /// <param name="user"></param>
        private void CheckVal(Models.FeatureDataModel featureData, string user)
        {
            if (string.IsNullOrEmpty(user))
            {
                new Exception("缺少用户信息");
            }
            if (featureData == null)
            {
                new Exception("缺少固件信息");
            }
            data = featureData.BinaryData;
        }
    }
}
